package webapp.frontOffice.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import modele.contrat.Contrat;
import modele.gestionVente.Enchere;
import modele.gestionVente.VenteDirecte;
import modele.utilisateur.Entreprise;
import modele.utilisateur.MembreSociete;
import modele.utilisateur.Utilisateur;
import dao.contrat.ActionDAO;
import dao.contrat.ContratDAO;
import dao.contrat.OptionCDAO;
import dao.gestionVente.EnchereDAO;
import dao.gestionVente.VenteDirecteDAO;

/**
 * Servlet implementation class ActionVendre
 */
@WebServlet("/action/vendre")
public class ActionVendre extends HttpServlet {
	private static final long serialVersionUID = 1L;


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/viewFrontoffice/actionvendre.jsp");
        dispatcher.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		HttpSession session = request.getSession();
		
		if(session == null || session.getAttribute("user") == null || !session.getAttribute("type").equals("investisseur"))
		{
			response.sendRedirect("index.jsp");
		}
		else
		{
			//Var session init
			Utilisateur user = (Utilisateur) session.getAttribute("user");
			
			//Var POST init
			String typeContrat = request.getParameter("typecontrat");
			String typeVente = request.getParameter("typevente");
			double prix = Double.parseDouble(request.getParameter("prix"));
			Date dateEchance;
			
	        String[] temp = request.getParameter("dateecheance").split("/");
	        dateEchance = new GregorianCalendar(Integer.parseInt(temp[2]), Integer.parseInt(temp[1])-1, Integer.parseInt(temp[0])).getTime();
	        
	        long idcontrat = Long.parseLong(request.getParameter("idcontrat"));
	        
	        //Var new init
	        Contrat contrat = new ContratDAO().retrieve(idcontrat);
	        Enchere enchere = null;
	        VenteDirecte vente = null;
	        
	        if(typeVente.equals("enchere"))
	        {
	        	enchere = new Enchere();
	        	enchere.setDate_echeance(dateEchance);
	        	enchere.setDate_emission(new Date());
	        	enchere.setPrixEmission(prix);
	        	enchere.setVendeur(user);
	        	
	        	Set<Contrat> portefeuille = new HashSet<Contrat>();
	        	portefeuille.add(contrat);
	        	enchere.setPortefeuille(portefeuille);
	        	
	        }
	        else if(typeVente.equals("vente"))
	        {
	        	vente = new VenteDirecte();
	        	vente.setDate_echeance(dateEchance);
	        	vente.setDate_emission(new Date());
	        	vente.setPrix(prix);
	        	vente.setVendeur(user);
	        	
	        	Set<Contrat> portefeuille = new HashSet<Contrat>();
	        	portefeuille.add(contrat);
	        	vente.setPortefeuille(portefeuille);
	        }
	        
	        if(vente != null)	new VenteDirecteDAO().create(vente);
	        if(enchere != null)	new EnchereDAO().create(enchere);
	        
	        response.sendRedirect(request.getContextPath() + "/offre/liste");
			
		}
	}

}
